/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OptimizerFrame.java
 *
 * Created on 2010-10-13, 18:22:21
 */

package nsdh.view;

import nsdh.model.OptimizerInput;
import nsdh.*;
import nsdh.model.script_models.*;
import javax.swing.table.*;
import javax.swing.*;
import java.util.*;
import java.beans.*;
import nsdh.model.script_models.*;
import javax.swing.event.*;

/**
 *
 * @author vtq
 */
public class OptimizerFrame extends javax.swing.JFrame{

    /** Creates new form OptimizerFrame */
    public OptimizerFrame(NsdhController nsdhController, NsdhGUI nsdhGUI) {
        initComponents();
        setVisible(true);
        this.setLocationRelativeTo(null);

        this.nsdhController = nsdhController;
        this.nsdhGUI = nsdhGUI;
        
        createFunctionArguementsTable();
        createVariablesTable();
        loadOptimizerInputFromNsdhModel();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        startButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        targetFunctionPanel = new javax.swing.JPanel();
        targetFunctionScrollPane = new javax.swing.JScrollPane();
        targetFunctionTextArea = new javax.swing.JTextArea();
        algorithmParamsPanel = new javax.swing.JPanel();
        numberOfStartRandomPointsLabel = new javax.swing.JLabel();
        numberOfStepRandomPointsLabel = new javax.swing.JLabel();
        numberOfStartRandomPointsTextField = new javax.swing.JTextField();
        numberOfStepRandomPointsTextField = new javax.swing.JTextField();
        maxIterationsLabel = new javax.swing.JLabel();
        maxIterationsTextField = new javax.swing.JTextField();
        maxTriesToFindPLessThanMNumberLabel = new javax.swing.JLabel();
        maxTriesToFindPLessThanMNumberTextField = new javax.swing.JTextField();
        variablesPanel = new javax.swing.JPanel();
        variablesScrollPane = new javax.swing.JScrollPane();
        variablesTable = new javax.swing.JTable();
        addVariablesButton = new javax.swing.JButton();
        removeVariablesButton = new javax.swing.JButton();
        functionArguementsPanel = new javax.swing.JPanel();
        functionArguementsScrollPane = new javax.swing.JScrollPane();
        functionArguementsTable = new javax.swing.JTable();
        addFunctionArguementsButton = new javax.swing.JButton();
        removeFunctionArguementsButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Optymalizacja");

        startButton.setText("Start");
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Anuluj");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        targetFunctionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Funkcja celu (minimalizacja)"));

        targetFunctionTextArea.setColumns(20);
        targetFunctionTextArea.setRows(3);
        targetFunctionScrollPane.setViewportView(targetFunctionTextArea);

        javax.swing.GroupLayout targetFunctionPanelLayout = new javax.swing.GroupLayout(targetFunctionPanel);
        targetFunctionPanel.setLayout(targetFunctionPanelLayout);
        targetFunctionPanelLayout.setHorizontalGroup(
            targetFunctionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(targetFunctionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(targetFunctionScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 952, Short.MAX_VALUE)
                .addContainerGap())
        );
        targetFunctionPanelLayout.setVerticalGroup(
            targetFunctionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(targetFunctionPanelLayout.createSequentialGroup()
                .addComponent(targetFunctionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        algorithmParamsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Parametry dodatkowe algorytmu optymalizacji"));

        numberOfStartRandomPointsLabel.setText("N - Liczba punktow próbnych w tablicy A:");

        numberOfStepRandomPointsLabel.setText("K - Liczba losowanych punktow z A uzywanych do wyznaczenia P:");

        maxIterationsLabel.setText("maxIter - Maksymalna liczba iteracji:");

        maxTriesToFindPLessThanMNumberLabel.setText("maxPTries - Maksymalna liczba prob wyznaczen P<M:");

        javax.swing.GroupLayout algorithmParamsPanelLayout = new javax.swing.GroupLayout(algorithmParamsPanel);
        algorithmParamsPanel.setLayout(algorithmParamsPanelLayout);
        algorithmParamsPanelLayout.setHorizontalGroup(
            algorithmParamsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(algorithmParamsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(algorithmParamsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(numberOfStartRandomPointsLabel)
                    .addComponent(numberOfStepRandomPointsLabel)
                    .addComponent(maxIterationsLabel)
                    .addComponent(maxTriesToFindPLessThanMNumberLabel))
                .addGap(37, 37, 37)
                .addGroup(algorithmParamsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(maxTriesToFindPLessThanMNumberTextField)
                    .addComponent(maxIterationsTextField)
                    .addComponent(numberOfStepRandomPointsTextField)
                    .addComponent(numberOfStartRandomPointsTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE))
                .addContainerGap(234, Short.MAX_VALUE))
        );
        algorithmParamsPanelLayout.setVerticalGroup(
            algorithmParamsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(algorithmParamsPanelLayout.createSequentialGroup()
                .addGroup(algorithmParamsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(algorithmParamsPanelLayout.createSequentialGroup()
                        .addComponent(numberOfStartRandomPointsLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(numberOfStepRandomPointsLabel))
                    .addGroup(algorithmParamsPanelLayout.createSequentialGroup()
                        .addComponent(numberOfStartRandomPointsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(numberOfStepRandomPointsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(algorithmParamsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxIterationsLabel)
                    .addComponent(maxIterationsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(algorithmParamsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(maxTriesToFindPLessThanMNumberLabel)
                    .addComponent(maxTriesToFindPLessThanMNumberTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        variablesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Zmienne optymalizacji, ich ograniczenia i wartosc poczatkowa"));

        variablesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        variablesTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
        variablesScrollPane.setViewportView(variablesTable);

        addVariablesButton.setText("Dodaj");
        addVariablesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addVariablesButtonActionPerformed(evt);
            }
        });

        removeVariablesButton.setText("Usun");
        removeVariablesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeVariablesButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout variablesPanelLayout = new javax.swing.GroupLayout(variablesPanel);
        variablesPanel.setLayout(variablesPanelLayout);
        variablesPanelLayout.setHorizontalGroup(
            variablesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, variablesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(variablesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 865, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(variablesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(removeVariablesButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(addVariablesButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        variablesPanelLayout.setVerticalGroup(
            variablesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(variablesPanelLayout.createSequentialGroup()
                .addGroup(variablesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(variablesPanelLayout.createSequentialGroup()
                        .addComponent(addVariablesButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeVariablesButton))
                    .addComponent(variablesScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
                .addContainerGap())
        );

        functionArguementsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Argumenty funkcji celu"));

        functionArguementsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        functionArguementsTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_LAST_COLUMN);
        functionArguementsScrollPane.setViewportView(functionArguementsTable);

        addFunctionArguementsButton.setText("Dodaj");
        addFunctionArguementsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addFunctionArguementsButtonActionPerformed(evt);
            }
        });

        removeFunctionArguementsButton.setText("Usun");
        removeFunctionArguementsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeFunctionArguementsButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout functionArguementsPanelLayout = new javax.swing.GroupLayout(functionArguementsPanel);
        functionArguementsPanel.setLayout(functionArguementsPanelLayout);
        functionArguementsPanelLayout.setHorizontalGroup(
            functionArguementsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, functionArguementsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(functionArguementsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 865, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(functionArguementsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(removeFunctionArguementsButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(addFunctionArguementsButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        functionArguementsPanelLayout.setVerticalGroup(
            functionArguementsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(functionArguementsPanelLayout.createSequentialGroup()
                .addGroup(functionArguementsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(functionArguementsPanelLayout.createSequentialGroup()
                        .addComponent(addFunctionArguementsButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeFunctionArguementsButton))
                    .addComponent(functionArguementsScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE))
                .addContainerGap())
        );

        saveButton.setText("Zapisz");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(algorithmParamsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(targetFunctionPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(variablesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(functionArguementsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(352, Short.MAX_VALUE)
                .addComponent(startButton, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(339, 339, 339))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(variablesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(functionArguementsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(targetFunctionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(algorithmParamsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(startButton)
                    .addComponent(saveButton)
                    .addComponent(cancelButton))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed

        try{

            
            this.saveOptimizerInputFromFrameDataToNsdhModel();
            this.nsdhController.updateOptimizerInput();

            nsdhGUI.graphPanel.setConsoleText("Poczatek optymalizacji...");
            

           nsdhGUI.optimizerProgressFrame = new OptimizerProgressFrame(nsdhController);

            nsdhController.optimizer.execute();
            nsdhGUI.optimizerProgressFrame.setVisible(true);
            this.dispose();


        }catch(Exception e){
            JOptionPane.showMessageDialog(this, e.getMessage());
        }finally{
            
            
        }

        //this.dispose();
    }//GEN-LAST:event_startButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void addFunctionArguementsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addFunctionArguementsButtonActionPerformed
        DefaultTableModel model = (DefaultTableModel)functionArguementsTable.getModel();
        Object[] newRow = {"",""};

        if(functionArguementsTable.isEditing()){
            functionArguementsTable.editingStopped(new ChangeEvent(addFunctionArguementsButton));
        }
        model.fireTableDataChanged();
        model.insertRow(model.getRowCount(), newRow);
        model.fireTableDataChanged();
        functionArguementsTable.requestFocusInWindow();

    }//GEN-LAST:event_addFunctionArguementsButtonActionPerformed

    private void addVariablesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addVariablesButtonActionPerformed
        DefaultTableModel model = (DefaultTableModel)variablesTable.getModel();
        Object[] newRow = {"","","","",""};

        if(variablesTable.isEditing()){
            variablesTable.editingStopped(new ChangeEvent(addVariablesButton));
        }
        model.fireTableDataChanged();
        model.insertRow(model.getRowCount(), newRow);
        model.fireTableDataChanged();
        variablesTable.requestFocusInWindow();
    }//GEN-LAST:event_addVariablesButtonActionPerformed

    private void removeFunctionArguementsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeFunctionArguementsButtonActionPerformed
        // TODO add your handling code here:
        DefaultTableModel model = (DefaultTableModel)functionArguementsTable.getModel();

        int[] selectedRows = functionArguementsTable.getSelectedRows();
        java.util.Arrays.sort(selectedRows);
        for(int i=selectedRows.length-1; i>= 0; i--){

            if(functionArguementsTable.isEditing()){
                functionArguementsTable.editingStopped(new ChangeEvent(removeFunctionArguementsButton));
            }
            model.fireTableDataChanged();
            model.removeRow(selectedRows[i]);
            model.fireTableDataChanged();
            functionArguementsTable.requestFocusInWindow();

        }
    }//GEN-LAST:event_removeFunctionArguementsButtonActionPerformed

    private void removeVariablesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeVariablesButtonActionPerformed
        DefaultTableModel model = (DefaultTableModel)variablesTable.getModel();

        int[] selectedRows = variablesTable.getSelectedRows();
        java.util.Arrays.sort(selectedRows);
        for(int i=selectedRows.length-1; i>= 0; i--){

            if(variablesTable.isEditing()){
                variablesTable.editingStopped(new ChangeEvent(removeVariablesButton));
            }
            model.fireTableDataChanged();
            model.removeRow(selectedRows[i]);
            model.fireTableDataChanged();
            variablesTable.requestFocusInWindow();

        }
    }//GEN-LAST:event_removeVariablesButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed



        saveOptimizerInputFromFrameDataToNsdhModel();
        this.dispose();
    }//GEN-LAST:event_saveButtonActionPerformed


    private void createFunctionArguementsTable(){
        //zainicjalizuj model tabeli
        String[] values = new String[]{"Nazwa", "Mapowanie na wartosc z modelu wynikow"};
        functionArguementsTable.setModel(new DefaultTableModel(values,0));

        //tworzy mape nazwa wyswietlana w combobox -> PC1 --> PC2 [VOIP]/avgDelay na server_client_connection:server_client_connection_name/param:avgDelay
        serverClientConnectionComboBoxMap = new HashMap<String,String>();
        for(Server_client_connection server_client_connection: nsdhController.nsdhModel.network_structure.server_client_connection_list.values()){
            String comboBoxLabel = server_client_connection.server_pc + " --> " + server_client_connection.client_pc + " [" + server_client_connection.service + "]/avgDelay [ms]";
            serverClientConnectionComboBoxMap.put(comboBoxLabel, "server_client_connection:"+server_client_connection.name+"/param:avgDelay");
            comboBoxLabel = server_client_connection.server_pc + " --> " + server_client_connection.client_pc + " [" + server_client_connection.service + "]/avgJitter [ms]";
            serverClientConnectionComboBoxMap.put(comboBoxLabel, "server_client_connection:"+server_client_connection.name+"/param:avgJitter");
            comboBoxLabel = server_client_connection.server_pc + " --> " + server_client_connection.client_pc + " [" + server_client_connection.service + "]/avgThroughput [kbps]";
            serverClientConnectionComboBoxMap.put(comboBoxLabel, "server_client_connection:"+server_client_connection.name+"/param:avgThroughput");
            comboBoxLabel = server_client_connection.server_pc + " --> " + server_client_connection.client_pc + " [" + server_client_connection.service + "]/packetLossRate";
            serverClientConnectionComboBoxMap.put(comboBoxLabel, "server_client_connection:"+server_client_connection.name+"/param:packetLossRate");
        }

        Set<String> comboBoxValuesNameSet = new HashSet(serverClientConnectionComboBoxMap.keySet());
        comboBoxValuesNameSet.add("");
        Object[] comboBoxValuesNameArray = comboBoxValuesNameSet.toArray();
        java.util.Arrays.sort(comboBoxValuesNameArray);

        //inicjalizuje combobox server-client-connection
        JComboBox modelMappingComboBox = new JComboBox(comboBoxValuesNameArray);

        //dodaje combox do tabeli
        functionArguementsTable.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(modelMappingComboBox));

        //ustawia szerokosc kolumny czas
        functionArguementsTable.getColumnModel().getColumn(0).setMaxWidth(80);
        functionArguementsTable.getColumnModel().getColumn(0).setPreferredWidth(80);

    }


    private void createVariablesTable(){

        //zainicjalizuj model tabeli
        String[] values = new String[]{"Nazwa", "Mapowanie na wartosc z modelu sieci","Min","Max","Start"};
        variablesTable.setModel(new DefaultTableModel(values,0));

        ArrayList<String> comboBoxValuesList = new ArrayList<String>();

        //pobierz wartosci z modelu do comboboxa
        for(Queue_policy queue_policy: nsdhController.nsdhModel.network_settings.queue_policy_list.values()){
            comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/param:mean_packet_size [B]");

            for(nsdh.model.script_models.Queue queue: queue_policy.queue_list){
                if(queue_policy.scheduler.equals("PRI")){
                    comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/scheduler_param:rate [bps]");
                }else if(queue_policy.scheduler.equals("WRR") || queue_policy.scheduler.equals("WIRR") || queue_policy.scheduler.equals("RR")){
                    comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/scheduler_param:weight");
                }

                for(String service_name: queue.policer_entry_list.keySet()){

                    if(queue.policer.equals("TSW2CM")){
                        comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/service:"+service_name+"/policer_param:CIR [bps]");
                    }else if(queue.policer.equals("TSW3CM")){
                        comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/service:"+service_name+"/policer_param:CIR [bps]");
                        comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/service:"+service_name+"/policer_param:PIR [bps]");
                    }else if(queue.policer.equals("TokenBucket")){
                        comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/service:"+service_name+"/policer_param:CIR [bps]");
                        comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/service:"+service_name+"/policer_param:CBS [B]");
                    }else if(queue.policer.equals("srTCM")){
                        comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/service:"+service_name+"/policer_param:CIR [bps]");
                        comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/service:"+service_name+"/policer_param:CBS [B]");
                        comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/service:"+service_name+"/policer_param:EBS [B]");
                    }else if(queue.policer.equals("trTCM")){
                        comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/service:"+service_name+"/policer_param:CIR [bps]");
                        comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/service:"+service_name+"/policer_param:CBS [B]");
                        comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/service:"+service_name+"/policer_param:PIR [bps]");
                        comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/service:"+service_name+"/policer_param:PBS [B]");
                    }


                }

                

                for(Subqueue subqueue: queue.subqueue_list){
                    comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/subqueue:"+queue.subqueue_list.indexOf(subqueue)+"/param:min_threshold");
                    comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/subqueue:"+queue.subqueue_list.indexOf(subqueue)+"/param:max_threshold");
                    comboBoxValuesList.add("queue_policy:"+queue_policy.name+"/queue:"+queue.name+"/subqueue:"+queue.subqueue_list.indexOf(subqueue)+"/param:probability_dropping");
                }
            }

        }

        //pomocnicza mapa, aby usuwac [bps] [b] ze stringa mapowania pozniej
        for(String comboBoxValue: comboBoxValuesList){
            if(comboBoxValue.contains(" [")){
                optimizationVariableMappingComboBoxMap.put(comboBoxValue.split(" \\[")[0], comboBoxValue);
            }else{
                optimizationVariableMappingComboBoxMap.put(comboBoxValue, comboBoxValue);
            }

        }


        Set<String> comboBoxValuesNameSet = new HashSet(comboBoxValuesList);
        comboBoxValuesNameSet.add("");
        Object[] comboBoxValuesNameArray = comboBoxValuesNameSet.toArray();
        java.util.Arrays.sort(comboBoxValuesNameArray);

        //inicjalizuje combobox server-client-connection
        JComboBox modelMappingComboBox = new JComboBox(comboBoxValuesNameArray);

        //dodaje combox do tabeli
        variablesTable.getColumnModel().getColumn(1).setCellEditor(new DefaultCellEditor(modelMappingComboBox));

        //ustawia szerokosc kolumn
        variablesTable.getColumnModel().getColumn(0).setMaxWidth(80);
        variablesTable.getColumnModel().getColumn(0).setPreferredWidth(80);

        variablesTable.getColumnModel().getColumn(2).setMaxWidth(60);
        variablesTable.getColumnModel().getColumn(2).setPreferredWidth(60);
        variablesTable.getColumnModel().getColumn(3).setMaxWidth(60);
        variablesTable.getColumnModel().getColumn(3).setPreferredWidth(60);
        variablesTable.getColumnModel().getColumn(4).setMaxWidth(60);
        variablesTable.getColumnModel().getColumn(4).setPreferredWidth(60);

    }





    private void saveOptimizerInputFromFrameDataToNsdhModel(){

        //koncz edycje komorek, jesli jakas jest edytowana

        if(functionArguementsTable.getCellEditor()!=null){
            functionArguementsTable.getCellEditor().stopCellEditing();
        }
        if(variablesTable.getCellEditor()!=null){
            variablesTable.getCellEditor().stopCellEditing();
        }

        Optimizer_input optimizer_input = new Optimizer_input();

        //koncz edycje komorek jesli jakies sa edytowane
        if(functionArguementsTable.getCellEditor()!=null){
            functionArguementsTable.getCellEditor().stopCellEditing();
        }
        if(variablesTable.getCellEditor()!=null){
            variablesTable.getCellEditor().stopCellEditing();
        }
        

        //dodaj liste argumentow (w tym mapowanie combobox z PC1->PC2 [VOIP]\avgDelay na server_client_connection:voip1/param:avgDelay)
        DefaultTableModel functionArguementsTableModel = (DefaultTableModel)functionArguementsTable.getModel();
        for(int i=0; i< functionArguementsTableModel.getRowCount();i++){
            Target_function_arguement target_function_arguement = new Target_function_arguement();
            target_function_arguement.mapping_expression = serverClientConnectionComboBoxMap.get((String)functionArguementsTableModel.getValueAt(i, 1));
            target_function_arguement.name = (String)functionArguementsTableModel.getValueAt(i, 0);
            optimizer_input.target_function_arguement_list.put(target_function_arguement.name,target_function_arguement);
        }

        //dodaj liste zmiennych
        DefaultTableModel variablesTableModel = (DefaultTableModel)variablesTable.getModel();
        for(int i=0; i< variablesTableModel.getRowCount();i++){

            Optimization_variable optimization_variable = new Optimization_variable();
            optimization_variable.name=(String)variablesTableModel.getValueAt(i, 0);
            optimization_variable.mapping_expression=((String)variablesTableModel.getValueAt(i, 1)).split(" \\[")[0]; //wytnij znaczniki [b] [kbps] itp
            optimization_variable.min_value=(String)variablesTableModel.getValueAt(i, 2);
            optimization_variable.max_value=(String)variablesTableModel.getValueAt(i, 3);
            optimization_variable.start_value=(String)variablesTableModel.getValueAt(i, 4);

            optimizer_input.optimization_variable_list.put(optimization_variable.name, optimization_variable);
        }

        //reszta paramatrow
        optimizer_input.target_function = targetFunctionTextArea.getText();
        optimizer_input.maxIter_param = maxIterationsTextField.getText();
        optimizer_input.N_param = numberOfStartRandomPointsTextField.getText();
        optimizer_input.K_param = numberOfStepRandomPointsTextField.getText();
        optimizer_input.maxPTries_param = maxTriesToFindPLessThanMNumberTextField.getText();

        this.nsdhController.nsdhModel.optimizer_input = optimizer_input;

    }

    private void loadOptimizerInputFromNsdhModel(){

        //sortowanie kluczy
        Object functionArguementsKeyObjectArray[] = nsdhController.nsdhModel.optimizer_input.target_function_arguement_list.keySet().toArray();
        String functionArguementsKeyArray[] = Arrays.copyOf(functionArguementsKeyObjectArray, functionArguementsKeyObjectArray.length, String[].class);
        java.util.Arrays.sort(functionArguementsKeyArray);

        //wczytaj liste argumentow (w tym mapowanie combobox z PC1->PC2 [VOIP]\avgDelay na server_client_connection:voip1/param:avgDelay)
        DefaultTableModel functionArguementsTableModel = (DefaultTableModel)functionArguementsTable.getModel();
        for(String targetFunctionArguementKey: functionArguementsKeyArray){
            Target_function_arguement target_function_arguement = this.nsdhController.nsdhModel.optimizer_input.target_function_arguement_list.get(targetFunctionArguementKey);
            String[] target_function_arguementArray = new String[2];
            target_function_arguementArray[0] = target_function_arguement.name;
            for (String mapKey: serverClientConnectionComboBoxMap.keySet()){
                if(serverClientConnectionComboBoxMap.get(mapKey).equals(target_function_arguement.mapping_expression)){
                    target_function_arguementArray[1] = mapKey;
                }
            }
            functionArguementsTableModel.addRow(target_function_arguementArray);
        }




        //sortowanie kluczy
        Object variableKeyObjectArray[] = nsdhController.nsdhModel.optimizer_input.optimization_variable_list.keySet().toArray();
        String variableKeyArray[] = Arrays.copyOf(variableKeyObjectArray, variableKeyObjectArray.length, String[].class);
        java.util.Arrays.sort(variableKeyArray);

        //dodaj liste zmiennych
        DefaultTableModel variablesTableModel = (DefaultTableModel)variablesTable.getModel();
        for(String variableKey: variableKeyArray){
            Optimization_variable optimization_variable =  this.nsdhController.nsdhModel.optimizer_input.optimization_variable_list.get(variableKey);
            String[] optimization_variableArray = new String[5];
            optimization_variableArray[0] = optimization_variable.name;
            optimization_variableArray[1] = optimizationVariableMappingComboBoxMap.get(optimization_variable.mapping_expression); //aby usunac [bps] i [b]
            optimization_variableArray[2] = optimization_variable.min_value;
            optimization_variableArray[3] = optimization_variable.max_value;
            optimization_variableArray[4] = optimization_variable.start_value;
            variablesTableModel.addRow(optimization_variableArray);
        }

        //reszta parametrow
        targetFunctionTextArea.setText(this.nsdhController.nsdhModel.optimizer_input.target_function);
        numberOfStartRandomPointsTextField.setText(this.nsdhController.nsdhModel.optimizer_input.N_param);
        numberOfStepRandomPointsTextField.setText(this.nsdhController.nsdhModel.optimizer_input.K_param);
        maxTriesToFindPLessThanMNumberTextField.setText(this.nsdhController.nsdhModel.optimizer_input.maxPTries_param);
        maxIterationsTextField.setText(this.nsdhController.nsdhModel.optimizer_input.maxIter_param);
    }

    



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addFunctionArguementsButton;
    private javax.swing.JButton addVariablesButton;
    private javax.swing.JPanel algorithmParamsPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JPanel functionArguementsPanel;
    private javax.swing.JScrollPane functionArguementsScrollPane;
    private javax.swing.JTable functionArguementsTable;
    private javax.swing.JLabel maxIterationsLabel;
    private javax.swing.JTextField maxIterationsTextField;
    private javax.swing.JLabel maxTriesToFindPLessThanMNumberLabel;
    private javax.swing.JTextField maxTriesToFindPLessThanMNumberTextField;
    private javax.swing.JLabel numberOfStartRandomPointsLabel;
    private javax.swing.JTextField numberOfStartRandomPointsTextField;
    private javax.swing.JLabel numberOfStepRandomPointsLabel;
    private javax.swing.JTextField numberOfStepRandomPointsTextField;
    private javax.swing.JButton removeFunctionArguementsButton;
    private javax.swing.JButton removeVariablesButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton startButton;
    private javax.swing.JPanel targetFunctionPanel;
    private javax.swing.JScrollPane targetFunctionScrollPane;
    private javax.swing.JTextArea targetFunctionTextArea;
    private javax.swing.JPanel variablesPanel;
    private javax.swing.JScrollPane variablesScrollPane;
    private javax.swing.JTable variablesTable;
    // End of variables declaration//GEN-END:variables

    private NsdhController nsdhController;
    private HashMap<String,String> serverClientConnectionComboBoxMap;
    private HashMap<String,String> optimizationVariableMappingComboBoxMap = new HashMap<String,String>();

    public NsdhGUI nsdhGUI;

    private ProgressMonitor progressMonitor;

}
